package com.android.inputmethod.pinyin;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.Button;

/**
 * Created by Admin on 2018/2/5.
 */

public class NineKeyButton extends Button {


    public String upperText;
    private Drawable src;

    public boolean isRepetable() {
        return repetable;
    }

    public Drawable getSrc() {
        return src;
    }

    public void setSrc(Drawable src) {
        this.src = src;
    }



    public void setRepetable(boolean repetable) {
        this.repetable = repetable;
    }

    public boolean repetable = false;
    public NineKeyButton(Context context)
    {
        super(context,null);
    }
    public NineKeyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        setUpperText(attrs);
    }
    public NineKeyButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr, 0);
        setUpperText(attrs);
    }
    public NineKeyButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        setUpperText(attrs);
    }

    public String getUpperText() {
        return upperText;
    }



    public void setUpperText(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs,R.styleable.NineKeyButton);
        this.src = typedArray.getDrawable(R.styleable.NineKeyButton_src);
        this.lastScale = typedArray.getFloat(R.styleable.NineKeyButton_src_scale,0.4f);
        this.upperText = typedArray.getString(R.styleable.NineKeyButton_upper_text);
        this.repetable = typedArray.getBoolean(R.styleable.NineKeyButton_repetable,false);
        typedArray.recycle();

    }

    private Bitmap bitmap;
    private float lastScale = 0.5f;
    private float scale = 0.3f;

    public void setUpperText(String upperText) {
        this.upperText = upperText;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        float width = getWidth();
        float height = getHeight();
        if(src != null)
        {
            if(bitmap == null)
            {
                BitmapDrawable bitmapDrawable = (BitmapDrawable) src;
                bitmap = bitmapDrawable.getBitmap();
                if(bitmap.getWidth() < bitmap.getHeight())
                    scale = height*lastScale / bitmap.getHeight();
                else
                    scale = width*lastScale / bitmap.getWidth();
                Matrix matrix = new Matrix();
                matrix.postScale(scale,scale);
                bitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
            }
            canvas.drawBitmap(bitmap,
                    (1-lastScale)*0.5f*width,(1-lastScale*0.5f)*0.5f*height,getPaint());
        }
        if(upperText == null)
        {
            super.onDraw(canvas);
            return;
        }
        Paint paint = new Paint();
        paint.setTextSize(getPaint().getTextSize()*0.8f);
        paint.setColor(getResources().getColor(R.color.upper_text_color));
        Rect textRect = new Rect();
        paint.getTextBounds(upperText,0,upperText.length(),textRect);
        canvas.drawText(upperText,width*0.5f-textRect.width()*0.5f,
                height*0.5f-paint.getTextSize()*0.8f,paint);
        super.onDraw(canvas);
    }
}
